package com.example.administrator.activitytest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data_return","Hello FirstActivity");
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("SecondActivity","Task id is "+getTaskId());
        setContentView(R.layout.second_layout);

        /*
            首先通过getIntent()方法获取用于启动SecondActivity的Intent
            然后调用getStringExtra()，传入相应的键值，就可以得到传递的数据
         */
        /*
            Intent intent = getIntent();
            String data = intent.getStringExtra("extra_data");
            Log.d("SecondActivity",data);
        */

        Button button2 = (Button)findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                    此处的Intent用于传递数据，没有任何的“意图”
                    然后调用setResult()方法，专门用于向上一个活动返回数据
                    其第一个参数用于向上一个活动返回处理结果
                    第二个参数则把带有数据的Intent传递回去
                 */
                /*
                    Intent intent = new Intent();
                    intent.putExtra("data_return","Hello FirstActivity");
                    setResult(RESULT_OK,intent);
                    finish();
                */
                Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("SecondActivity","onDestory");
    }
}
